Skip to content

字符串编码测试 - EncodeTest

函数简介

将输入字符串按 DefaultEncoding 解码为内部 UTF-8,再按 DefaultReturnEncoding 编码后原样返回,用于快速验证编码配置是否正确。

相关配置见 SetDefaultEncodeSetConfigByKey 中的 DefaultEncoding / DefaultReturnEncoding

接口名称

EncodeTest

DLL调用

c
long EncodeTest(long instance, string input);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
input字符串待测试的字符串。按当前 DefaultEncoding(入参编码)传入。

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
std::string result = ola.EncodeTest("欧拉插件编码测试");
// 配置正确时,result 应与 input 语义一致
csharp
using OLAPlug;

var ola = new OLAPlugServer();
string result = ola.EncodeTest("欧拉插件编码测试");
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
result = ola.EncodeTest("欧拉插件编码测试")
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
String result = ola.EncodeTest("欧拉插件编码测试");
go
import "github.com/ola/olaplug/olaplug"

ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
result := ola.EncodeTest("欧拉插件编码测试")
// 配置正确时,result 应与 input 语义一致
rust
use olaplug::OLAPlugServer;

let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let result = ola.encode_test("欧拉插件编码测试");
// 配置正确时,result 应与 input 语义一致
cpp
var ola = com("OlaPlug.OlaSoft")
var result = ola.EncodeTest("欧拉插件编码测试")
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
result = ola.EncodeTest("欧拉插件编码测试")
text
.局部变量 ola, OLAPlug
ola.创建 ()
result = ola.EncodeTest("欧拉插件编码测试")
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var result = ola.EncodeTest("欧拉插件编码测试");
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
文本型 result = ola.EncodeTest("欧拉插件编码测试")
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
std::string result = ola.EncodeTest("欧拉插件编码测试");

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long ptr = EncodeTest(instance, "欧拉插件编码测试");
if (ptr != 0) {
    char buffer[512] = {0};
    GetStringFromPtr(ptr, buffer, sizeof(buffer));
    FreeStringPtr(ptr);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long EncodeTest(long ola, string input);

long instance = CreateCOLAPlugInterFace();
long ptr = EncodeTest(instance, "欧拉插件编码测试");
if (ptr != 0) {
    StringBuilder sb = new StringBuilder(GetStringSize(ptr) + 1);
    GetStringFromPtr(ptr, sb, sb.Capacity);
    FreeStringPtr(ptr);
    string result = sb.ToString();
}
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
ptr = ola.EncodeTest(instance, "欧拉插件编码测试")
if ptr:
    buf = create_string_buffer(512)
    ola.GetStringFromPtr(ptr, buf, 512)
    ola.FreeStringPtr(ptr)
    result = buf.value.decode("utf-8")

返回值

返回值说明
(返回值)字符串指针:经 DefaultEncoding 解码、再按 DefaultReturnEncoding 编码后的结果。DLL 调用返回字符串指针地址,需调用 FreeStringPtr 释放内存。配置正确时,返回值应与 input 在语义上一致。

注意事项

项目说明
释放内存返回的字符串指针需调用 FreeStringPtr 释放内存。
默认编码默认入参 GBKDefaultEncoding=0)、出参 UTF-8DefaultReturnEncoding=1);亦可通过 SetDefaultEncodeSetConfigByKey 修改。
验证方式将返回值与 input 比对;一致则说明当前编码配置正确,乱码则说明需调整编码设置。
编码取值0=GBK,1=UTF-8,2=Unicode,与 SetDefaultEncode 一致。